<html>
<head>
<script>
var spl;
var lastTabUrl;
var lastTabId;
var lastTabHaveContentScript=false;
var targets = {};
var optionid;
var img = new Image();
img.src = chrome.extension.getURL("next.png");

function checkUrl(url)
{
  spl=url.split(/(\d+)/);
  for(var i=0;i<spl.length;i++)
  {
    if (!isNaN(spl[i]))
    {
      return true;
    }
  }
  return false;
}

function checkTabid(tabid)
{
  chrome.tabs.get(tabid,function(tab)
  {
    if (tab.url.indexOf(chrome.extension.getURL(""))==-1)
    {
      if (optionid) 
      {
        chrome.tabs.remove(optionid);
        optionid=null;
      }
      lastTabUrl=tab.url;
      lastTabId=tab.id;

      if (checkUrl(lastTabUrl))
      {
        chrome.pageAction.show(tab.id);
        updateIcon(tab.id,0,0);
        lastTabHaveContentScript=false;
      } else
        chrome.pageAction.hide(tab.id);
      chrome.tabs.sendRequest(tab.id,{command:"geturls",force:false},function(response) 
      {
        lastTabHaveContentScript=true;
        if (response.prev=="" && response.next=="")
        {
        } else
        {
          chrome.pageAction.show(tab.id);
          updateIcon(tab.id,response.prev==""?1:2,response.next==""?1:2);
        }
      });

    } else
    {
      optionid=tab.id;
      chrome.pageAction.hide(tab.id);
    }
  });
}

function updateIcon(tabid,prevMark,nextMark)
{
  var targetNum=targets[tabid];
  if (isNaN(targetNum) || targetNum.length==0) targetNum=1;
  var ctx = document.getElementById('canvas').getContext('2d');
  ctx.clearRect(0,0,19,19);
  ctx.save();
  if (targetNum>=0)
  {
    chrome.pageAction.setTitle({title: "Next Page", tabId: tabid})
  } else 
  {
    chrome.pageAction.setTitle({title: "Previous Page", tabId: tabid})
    ctx.translate(19, 0);
    ctx.scale(-1,1);
  }
  if (Math.abs(targetNum)!=1 && prevMark==0 && nextMark==0)ctx.globalAlpha=0.35;
  ctx.drawImage(img,0,0);
  ctx.restore();
  if (Math.abs(targetNum)!=1 && prevMark==0 && nextMark==0)
  {
    ctx.font="bold 12px sans-serif";
    ctx.textAlign="right";
    ctx.fillText(""+Math.abs(targetNum),17,17);
    ctx.restore();
  }
  ctx.globalAlpha=0.5;
  if (prevMark>0)
  {
    ctx.beginPath();
    ctx.fillStyle=prevMark==1?"#FF0000":"#0000FF";
    ctx.arc(3,3,3,0,Math.PI*2,true);
    ctx.closePath();
    ctx.fill();
  }
  if (nextMark>0)
  {
    ctx.beginPath();
    ctx.fillStyle=nextMark==1?"#FF0000":"#0000FF";
    ctx.arc(16,3,3,0,Math.PI*2,true);
    ctx.closePath();
    ctx.fill();
  }
  ctx.globalAlpha=1;
  chrome.pageAction.setIcon({imageData: ctx.getImageData(0, 0, 19, 19), tabId: tabid});
}

chrome.tabs.onSelectionChanged.addListener(function(tabId) 
{
  checkTabid(tabId);
});

chrome.tabs.onUpdated.addListener(function(tabId) 
{
  checkTabid(tabId);
});

chrome.tabs.getSelected(null, function(tab) 
{
  checkTabid(tab.id);
});

function updateNumURL(ctab,val,targetNum)
{
  var newUrl="";
  var animation={command:"animnext",background:"rgba(127, 225, 127, 0.5)",from:-1,to:-1};
  if (checkUrl(ctab.url))
  {
    var countNum=0;
    for(var i=spl.length;i>0;i--)
    {
      var item=spl[i-1];
      if (!isNaN(item) && item.length)
      {
        if (++countNum==Math.abs(targetNum))
        {
          var itemLen=item.length;
          if (val==0)
          {
            if (targetNum>=0)item++; else item--;
          } else if (val==1)
          {
            item++; 
            animation.from=1;
            animation.to=0;
          } else if (val==-1)
          {
            item--;
            animation.from=-1;
            animation.to=0;
          }
          if (item<0)item=0;
          item=""+item;
          while (item.length<itemLen) item="0"+item;
        }
      }
      if(item)newUrl=item+newUrl;
    }
  }
  if (newUrl!="")
  {
    chrome.tabs.sendRequest(ctab.id,animation);
    chrome.tabs.update(ctab.id,{url:newUrl});
  }
}

function changeURL(ctab,val)
{
  var newUrl="";
  var targetNum=targets[ctab.id];
  if (isNaN(targetNum) || targetNum.length==0) targetNum=1;
  if (lastTabHaveContentScript)
  {
    chrome.tabs.sendRequest(ctab.id,{command:"geturls",force:true},function(response) 
    {
      if (response.prev=="" && response.next=="")
      {
        updateNumURL(ctab,val, targetNum);
      } else
      {
        var item=val;
        if (item==0) item=targetNum>=0?1:-1;
        var animation={command:"animnext",background:"rgba(127, 127, 225, 0.5)",from:-1,to:-1};
        if (item==-1)
        {
          newUrl=response.prev;
          animation.from=-1;
          animation.to=0;
        }
        if (item==1)
        {
          newUrl=response.next;
          animation.from=1;
          animation.to=0;
        }
        if (newUrl!="")
        {
          chrome.tabs.sendRequest(ctab.id,animation);
          chrome.tabs.update(ctab.id,{url:newUrl});
        }
      }
    });
  } else 
  {
    updateNumURL(ctab,val, targetNum);
  }
}

chrome.pageAction.onClicked.addListener(function(tab) 
{
  changeURL(tab,0);
});

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{
  changeURL(sender.tab,request);
});

</script>
</head>
<body>
<canvas id="canvas" width="19" height="19"></canvas>
</body>
</html>

